<?php
namespace app\common\model;

use think\Model;

/**
 * @Author: qian 
 * @Date:   2019-11-07 15:41:38
 * @Last Modified by:   qian
 * @Last Modified time: 2019-11-11 09:25:19
 */
class Rsa {

    // 下发秘钥 商户私钥  并且不能出现空格等多余字符【别用成交易秘钥】
    private static $prikey = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJAhGHcE0Kw7s+Tsx1/z9kaSpNj6ixlrGeg5Rg5h5aQ6rKTCChpF2N2u2fny28M3brSvr/wPMrHEyLVP0BDpp0Rx/eNnlBeIbUQfr/SPrp6IVBLu8PVYksv0jb5JBi4/LaxBoNyT+f56quhRthrr7/RRYE8c1RWAnYV0CkosZwK7AgMBAAECgYBp9tsczf55Ul+dZHPk2OFs2221VOalpndio5EdC96hEr5RV5PD4yxhN6dsu5/U89FBqJVTVP0pc4s4ulg3xwFBDo46dPZtEh3JR8nAwqjCgyPSCukwR7yc9DNcSHnT54qdWuKCDyGKHiwSW4FsCzB+E6VW4JcF0Oc6ynp+59bLOQJBAPegsqE6+jbvMu0cnRXFRnLAyK6WbhmqmVCIoW75Kc66mv7RjkLfXdekGDq0P7Fgp1eVL6ppj8zijYJwiJny/EcCQQCVAJVh2LfxIp8nlSaKY9CeBicJqNpIwg6PgoPC+b6qfJ2WWnSpyAopUEo2lCeCutGViJGGP7rP3j+9rKRf/GPtAkA6A6IV9/VLDXtG5d8/jHpWxCfAD2o4BSnqHjiEAUf0MO/DJ2FG9YHM1Zr/ld8eNWC/OipyqEySWbmBh8/ZEX5dAkBe35U+TSCqIFJYgMaefXm69M3sf6AljbJ5vC+reRvnMBujPYtlA6M8tQvb/q+9tLh4TxHh+jg33RqW6hNrrqftAkEAm+OVe1d7DDWU+Rb1KxIxyRw5fbPzmGk/gLnAPuKxqWeC/3D72KH26/JjzzeT6lmdOu/WH8YrlsRstKTQv4n5uA==';

    // 下发秘钥 一麻袋公钥   并且不能出现空格等多余字符
    private static $pubkey='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOGrUM5kcF0urwTV6JbhGitJLNNmQ6YA17oc7M83MAcAB+nOARQIX9z1DSjEWr/l8NrEUjF4ee8c9Z3KvCuCuJexVH5aSsZR56IljxBaW2RRPlIf0JUBCuWbyznfMVraxnsdFETwKahKSa53AlFl1Kd4V+r6I/D5OJlptNbCegfQIDAQAB';
    
    public static function _redPubkey() {
        $pem = "-----BEGIN PUBLIC KEY-----\n" . chunk_split(self::$pubkey, 64, "\n") . "-----END PUBLIC KEY-----\n";
        return openssl_pkey_get_public($pem);
    }

    public static function _redPrikey() {
        $pem = "-----BEGIN RSA PRIVATE KEY-----\n" . chunk_split(self::$prikey, 64, "\n") . "-----END RSA PRIVATE KEY-----\n";
        return openssl_pkey_get_private($pem);
    }



    /**     
     * 获取私钥     
     * @return bool|resource     
     */    
    private static function getPrivateKey() 
    {        
        return self::_redPrikey(); 
    }    

    /**     
     * 获取公钥     
     * @return bool|resource     
     */    
    private static function getPublicKey()
    {   
        return self::_redPubkey();
    }

    /**     
     * 私钥加密     
     * @param string $data     
     * @return null|string     
     */    
    public static function privEncrypt($data = '')    
    {        
        if (!is_string($data)) {            
            return null;       
        }        
        return openssl_sign($data,$encrypted,self::getPrivateKey()) ? base64_encode($encrypted) : null;    
    }    

    /**     
     * 公钥加密     
     * @param string $data     
     * @return null|string     
     */    
    public static function publicEncrypt($data = '')   
    {        
        if (!is_string($data)) {            
            return null;        
        }        
        // dump($data);
        return openssl_public_encrypt($data,$encrypted,self::getPublicKey()) ? base64_encode($encrypted) : null;    
    }    

    /**     
     * 私钥解密     
     * @param string $encrypted     
     * @return null     
     */    
    public static function privDecrypt($encrypted = '')    
    {        
        if (!is_string($encrypted)) {            
            return null;        
        }        
        return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey())) ? $decrypted : null;    
    }    

    /**     
     * 公钥解密     
     * @param string $encrypted     
     * @return null     
     */    
    public static function publicDecrypt($encrypted = '')    
    {        
        if (!is_string($encrypted)) {            
            return null;        
        }        
    return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey())) ? $decrypted : null;    
    }

}